package net.vonkannen.callduration;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.IBinder;
import android.provider.CallLog;
import android.text.format.Time;

public class CallDurationService extends Service {
	private static final String PREFS_NAME = "CallDuration_prefs";

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	

    Calendar durationCalendar = Calendar.getInstance();
    SimpleDateFormat durationFormat = new SimpleDateFormat("KK:mm:ss");	
    @Override
	public void onStart(Intent intent, int startid) {	/* Called on onhook */

//		Log.v("MyActivity", "Starting Service!");
		
		/* Set up time formatter */
		durationCalendar.setTimeZone(TimeZone.getTimeZone("UTF"));
		durationFormat.setTimeZone(durationCalendar.getTimeZone());
		
		/* Set up cursor to read out duration */
		Cursor c = getContentResolver().query(
	               android.provider.CallLog.Calls.CONTENT_URI,
	               null,
	               null,
	               null,
	               android.provider.CallLog.Calls.DATE + " DESC");
		c.moveToFirst();
		
		/* Read out duration */
		long duration = c.getLong(c.getColumnIndex(CallLog.Calls.DURATION));
		if(duration>=0) {
			/* Create output for toast */
			durationCalendar.setTimeInMillis(duration*1000);
			String durationOutput = getString(R.string.duration) + " " + durationFormat.format(durationCalendar.getTime());

	        /* Read SharedPreferences */
	        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

			/* Check if keyguard is active */
			/* When keyguard is active, set ShowLastCall to true and set LastCallTime to current time  */
			KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
			SharedPreferences.Editor editor = settings.edit();
			if(keyguardManager.inKeyguardRestrictedInputMode()) {	/* Show toast, when Keyguard is disabled within 1min (See PhoneService*.java) */
				editor.putBoolean("ShowLastCall", true);
				Time timer_ = new Time();	/* Set time when onhook */
				timer_.setToNow();
				editor.putLong("LastCallTime", timer_.toMillis(true));
				//Log.v("DebugTag", "Display is locked");
				//Log.v("DebugTag", "ShowLastCall=true");
				//Log.v("DebugTag", "Time="+timer_.toMillis(true));
			}
			else {	/* Show toast immediately */
				editor.putBoolean("ShowLastCall", false);
				editor.putBoolean("NewCall", true);
				//Log.v("DebugTag", "Display is unlocked");
				
		        
		        /* Create and show toast */
				CustomToast toast = new CustomToast(getApplicationContext(),
						durationOutput,
						Integer.parseInt(settings.getString("radius", "4").toString()),
						Integer.parseInt(settings.getString("borderthickness", "6").toString()),
						(int) Long.parseLong(settings.getString("bordercolor", "FFABABAB").toString(), 16),
						(int) Long.parseLong(settings.getString("bgcolor", "FA444444").toString(), 16),
						(int) Long.parseLong(settings.getString("fontcolor", "FFFFFFFF").toString(), 16),
						settings.getBoolean("showicon", true));
				toast.show();
				
	//			Log.i(getString(R.string.app_name), durationOutput);
			}
			editor.commit();
		}
		Intent stop = new Intent("net.vonkannen.callduration.CallDurationService");
		this.stopService(stop);
	}
	
}
